// todo 1-13 路由
import Vue from 'vue'
// todo 1-13-1 路由页面
import VueRouter from 'vue-router'
import store from '@/store/index'
import { getLocalToken } from '@/utils/token'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const login = () => import('../views/login/Login')
const my = () => import('../views/home/my/My')
const info = () => import('../views/home/info/Info')
const home = () => import('../views/home/Home')
const index = () => import('../views/home/index/Index')
const search = () => import('../views/home/search/Search')
const searchResult = () => import('../views/home/search/SearchResult')
const detail = () => import('../views/home/detial/Detail')
const video = () => import('../views/home/video/Video')

const notFound = () => import('../views/404/NotFound.vue')

// todo 1-13-3 修复路由潜在bug

Vue.use(VueRouter)
// todo 1-13-2 定义路由规则
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: login
  },
  {
    path: '/home',
    name: 'Home',
    component: home,
    redirect: '/home/index',
    children: [
      {
        path: '/home/index',
        name: 'Index',
        component: index,
        meta: {
          isNeedLogin: false,
          isNeedTab: true
        }
      },
      {
        path: '/home/my',
        name: 'My',
        component: my,
        meta: {
          isNeedLogin: true,
          isNeedTab: true
        }
      },
      {
        path: '/home/info',
        name: 'Info',
        component: info,
        meta: {
          isNeedLogin: true,
          isNeedTab: false
        }
      },
      {
        path: '/home/search',
        name: 'Search',
        component: search,
        meta: {
          isNeedLogin: false,
          isNeedTab: true
        }
      },
      {
        path: '/home/searchResult',
        name: 'SearchResult',
        component: searchResult,
        meta: {
          isNeedLogin: false,
          isNeedTab: false
        }
      },
      {
        path: '/home/detail',
        name: 'Detail',
        component: detail,
        meta: {
          isNeedLogin: false,
          isNeedTab: false
        }
      },
      {
        path: '/home/video',
        name: 'Video',
        component: video,
        meta: {
          isNeedLogin: false,
          isNeedTab: true
        }
      }
    ]
  },
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/404',
    name: 'NotFound',
    component: notFound
  }
]

const router = new VueRouter({
  routes
})

// todo 5-1 登录校验处理
/* 1.判断当前页面是否需要登录
*      不需要,放行
*      需要 2.判断用户是否登录
*               登录 放行
*               未登录  3. 判断用户是否有正确的token
*                          有，   获取用户信息， 设置登录状态为true  把用户信息保存到vuex中 放行
*                          没有，跳转到登录页面 next()
*
*
* */
router.beforeEach((to, from, next) => {
  // 记录每个页面离开时的滚动条位置的路由
  const _scrollTop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset
  from.meta.scrollTop = _scrollTop
  console.log('scrollTop', _scrollTop)
  if (!to.meta.isNeedLogin) {
    next()
  } else {
    if (store.isLogin) {
      next()
    } else {
      if (getLocalToken('token')) {
        store.commit('setIsLogin', true)
        next()
      } else {
        next({
          name: 'Login',
          params: {
            toPath: to.fullPath
          }
        })
      }
    }
  }
})

//  进入任何页面都会到顶部
// router.afterEach((to, from, next) => {
//   window.scrollTo(0, 0)
// })

export default router
